package cn.wolfcode.shop.cloud.util;

import cn.wolfcode.shop.cloud.domain.User;

import java.sql.*;

public class MyUtil {


    public static Connection getConn() throws Exception {
        //测试连接mycat
        String url = "jdbc:mysql://192.168.138.132:8066/TESTDB";
        String username = "root";
        String password = "123456";
        String driver = "com.mysql.jdbc.Driver";
        Class.forName(driver);
        return DriverManager.getConnection(url, username, password);
    }

    public static void main(String[] args) throws Exception {
       // insert(3L, "oppo手机");
          get(3L); //mycat的读写分离

    }

    public static void insert(Long orderNo, String name) throws Exception {
        Connection conn = MyUtil.getConn();
        String sql = "insert into t_order(orderNo, name)values(?,?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setLong(1, orderNo);
        pstmt.setString(2, name);
        pstmt.execute();
        pstmt.close();
        conn.close();
    }
    public  static void get(Long orderNo) throws Exception {
        Connection conn = MyUtil.getConn();
        String sql = "select *from t_order where orderNo=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setLong(1, orderNo);

        ResultSet resultSet = pstmt.executeQuery();
        while (resultSet.next()){
            long orderNo1 = resultSet.getLong("orderNo");
            String name = resultSet.getString("name");
            System.out.println(orderNo1 + name);
        }
        pstmt.close();
        conn.close();
    }

}
